package com.augurit.gzcgwsp.ywcmn.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SQLQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.ywcmn.convert.YwComSjcllistOtherConvertor;
import com.augurit.gzcgwsp.ywcmn.dao.YwComSjcllistOtherDao;
import com.augurit.gzcgwsp.ywcmn.entity.YwComSjcllistOther;
import com.augurit.gzcgwsp.ywcmn.service.IYwComSjcllistOtherService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComSjcllistForm;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComSjcllistOtherForm;
import com.augurit.wf.bi.service.IWfBusTemplateService;
import com.augurit.wf.bi.web.form.WfBusTemplateForm;

@Service
@Transactional
public class YwComSjcllistOtherServiceImpl implements IYwComSjcllistOtherService{

	@Autowired
	private YwComSjcllistOtherDao ywComSjcllistOtherDao;
    
    @Autowired
    private IWfBusTemplateService wfBusTemplateService;
	
	/**
	 * ��������ȡForm����
	 */
	@Transactional(readOnly = true)
	public YwComSjcllistOtherForm get(Long id) {
		YwComSjcllistOther entity = ywComSjcllistOtherDao.get(id);
		return YwComSjcllistOtherConvertor.convertVoToForm(entity);
	}
	
	/**
	 * ɾ��Form�����б�
	 */
	public void delete(Long... ids) {
		ywComSjcllistOtherDao.delete(ids);
	}

	/**
	 * �����������޸ĵ�Form�����б�
	 */
	public void save(YwComSjcllistOtherForm... forms) {
		if(forms != null)
			for(YwComSjcllistOtherForm form : forms)
				this.save(form);
	}
	
	/**
	 * �����������޸ĵ�Form����
	 */
	public void save(YwComSjcllistOtherForm form){
		
		if(form != null){
			YwComSjcllistOther entity = null;
			
			//׼��VO����
			if(form != null && form.getId() != null){
				entity = ywComSjcllistOtherDao.get(form.getId());
			}else{
				entity = new YwComSjcllistOther();
			}
			
			//����ֵת��
			YwComSjcllistOtherConvertor.convertFormToVo(form, entity);
			
			//����
			ywComSjcllistOtherDao.save(entity);
			
			//����ID����ֵ
			form.setId(entity.getId());
		}
	}

    @Transactional(readOnly = true)
	public List<YwComSjcllistOtherForm> getList(YwComSjcllistForm form) {
//        StringBuffer hql = new StringBuffer("from YwComSjcllistOther ps where ps.tplcode = '" + form.getTplcode() + "' order by ps.sortNum asc,ps.rowid asc");
//        List<YwComSjcllistOther> entityList = ywComSjcllistOtherDao.find(hql.toString());
//        List<YwComSjcllistOtherForm> formList = YwComSjcllistOtherConvertor.convertVoListToFormList(entityList);
//    	用sql查询
    	String sqlString = "select * from yw_com_sjcllist_other t where t.tplcode = '"+ form.getTplcode() + "' and t.ajid='"+form.getAjid()+"' order by t.sort_num asc,t.rowid asc";
    	SQLQuery sqlQuery = ywComSjcllistOtherDao.getSession().createSQLQuery(sqlString).addEntity(YwComSjcllistOther.class);
    	List<YwComSjcllistOther> entityList = sqlQuery.list();
    	List<YwComSjcllistOtherForm> formList = YwComSjcllistOtherConvertor.convertVoListToFormList(entityList);
    	return formList;
    }
    
    @Transactional(readOnly = true)
    public List<YwComSjcllistOtherForm> getList(String name, String templateCode) {
    	StringBuffer hql = new StringBuffer("from YwComSjcllistOther ps where ps.tplcode = '" + templateCode + "' and ps.tplcode like '%"+ name +"%'");
    	List<YwComSjcllistOther> entityList = ywComSjcllistOtherDao.find(hql.toString());
    	List<YwComSjcllistOtherForm> formList = YwComSjcllistOtherConvertor.convertVoListToFormList(entityList);
    	return formList;
    }

	/**
	 * ���Form����Ĳ�ѯ��������ҳ��ѯ
	 */
	@Transactional(readOnly = true)
	public Page<YwComSjcllistOtherForm> search(Page<YwComSjcllistOtherForm> page, YwComSjcllistOtherForm form) {
		// �����µķ�ҳ����
		Page pg = PageUtils.newPageInstance(page);
		
		// ��ѯ��估����
		StringBuffer hql = new StringBuffer("from YwComSjcllistOther ps where 1=1");
		List values = new ArrayList();
		
		// ��ѯ����
		if(form != null){
            if (StringUtils.isNotBlank(form.getTitle())) {
                hql.append(" and ps.title like '%" + form.getTitle() + "%'");
            }
            if (StringUtils.isNotBlank(form.getTplcode())) {
                hql.append(" and ps.tplcode = '" + form.getTplcode() + "'");
            }
		}
		
		//����
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		// ִ�з�ҳ��ѯ����
		ywComSjcllistOtherDao.findPage(pg, hql.toString(), values);
		
		// ת��ΪForm�����б?��ֵ��ԭ��ҳ������
		List<YwComSjcllistOtherForm> list = YwComSjcllistOtherConvertor.convertVoListToFormList(pg.getResult());
		
		if (list != null && list.size() != 0) for (YwComSjcllistOtherForm iform : list) {
		    WfBusTemplateForm tplForm = wfBusTemplateService.getByTemplateCode(iform.getTplcode());
		    if (tplForm != null) iform.setTplname(tplForm.getTemplateName());
		}
		
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * ��ݹ��������б�����ҳ��ѯ
	 */
	@Transactional(readOnly = true)
	public Page<YwComSjcllistOtherForm> search(Page<YwComSjcllistOtherForm> page, List<PropertyFilter> filters) {
		// �����µķ�ҳ����
		Page<YwComSjcllistOther> pg = PageUtils.newPageInstance(page);
		
		// ������������ҳ���Ҷ���
		ywComSjcllistOtherDao.findPage(pg, filters);
		
		// ת��ΪForm�����б?��ֵ��ԭ��ҳ������
		List<YwComSjcllistOtherForm> list = YwComSjcllistOtherConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
    public List<YwComSjcllistOther> getYwComSjcls(String templateCode) {
        String hql=" from "+YwComSjcllistOther.class.getName()+" where  tplcode='"+templateCode+"'";
        return ywComSjcllistOtherDao.find(hql);
    }
}
